//
//  DZMRMBottomView.swift
//  DZMeBookRead
//
//  Created by 邓泽淼 on 2017/5/11.
//  Copyright © 2017年 DZM. All rights reserved.
//

import UIKit
import ASValueTrackingSlider

class DZMRMBottomView: DZMRMBaseView {
    
    /// 进度
    private lazy var slider = ASValueTrackingSlider()
    
    private var buttonToggleLight:MenuButton?
    
    /// 功能按钮数组
    //"read_bar_3"
    private let funcIcons:[String] = ["menu_dir","menu_sun","menu_setting"]
    private let funcTitles:[String] = ["目录","日间","设置"]

    override func addSubviews() {
        super.addSubviews()
        
        // 创建按钮
        var preView:UIView?
        for index in 0 ..< funcIcons.count {
            let button = MenuButton(imageName: self.funcIcons[index], title: self.funcTitles[index])
            
            if index == 1 {
                self.buttonToggleLight = button
            }
            
            button.isUserInteractionEnabled = true
            button.tag = index
            let buttonTap = UITapGestureRecognizer(target: self, action: #selector(self.clickButton(ges:)))
            button.addGestureRecognizer(buttonTap)
            self.addSubview(button)
            button.snp.makeConstraints({ (make) in
                if let preView_ = preView {
                    make.left.equalTo(preView_.snp.right)
                    make.width.equalTo(preView_.snp.width)
                } else {
                    make.left.equalToSuperview()
                }
                
                if index >= self.funcIcons.count - 1 {
                    make.right.equalToSuperview()
                }
                
                make.top.equalToSuperview().offset(32.0)
                make.height.equalTo(80.0)
                
                // 适配iPhoneX
                if UIScreen.main.bounds.height == 812.0
                    || UIScreen.main.bounds.height == 896.0 {
                    make.bottom.equalToSuperview().offset(-34.0)
                } else {
                    make.bottom.equalToSuperview()
                }
                
            })
            
            preView = button
        }

        // 上一章
        let previousChapter = UIButton(type:.custom)
        previousChapter.titleLabel?.font = UIFont.system._12
        previousChapter.contentHorizontalAlignment = .right
        previousChapter.setTitle("上一章", for: .normal)
        previousChapter.setTitleColor(UIColor.white, for: .normal)
        previousChapter.addTarget(self, action: #selector(DZMRMBottomView.clickPreviousChapter), for: .touchUpInside)
        self.addSubview(previousChapter)
        
        // 下一章
        let nextChapter = UIButton(type:.custom)
        nextChapter.titleLabel?.font = UIFont.system._12
        nextChapter.contentHorizontalAlignment = .left
        nextChapter.setTitle("下一章", for: .normal)
        nextChapter.setTitleColor(UIColor.white, for: .normal)
        nextChapter.addTarget(self, action: #selector(DZMRMBottomView.clickNextChapter), for: .touchUpInside)
        self.addSubview(nextChapter)
        
        // 进度条
        self.slider.delegate = self
        let thumbImage = UIImage(named: "RM_3", in: Bundle(for: type(of: self)), compatibleWith: nil)
        self.slider.setThumbImage(thumbImage, for: .normal)
        self.slider.minimumValue = 1
        self.slider.maximumValue = 1
        self.slider.setMaxFractionDigitsDisplayed(0)
        self.slider.popUpViewColor = UIColor.cj.red
        self.slider.font = UIFont(name: "Futura-CondensedExtraBold", size: 22)
        self.slider.textColor = UIColor.white
        self.slider.popUpViewArrowLength = 10.0
        self.addSubview(self.slider)
        
        previousChapter.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(10.0)
            make.width.equalTo(55.0)
            make.height.equalTo(32.0)
        }
        
        nextChapter.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.top.equalToSuperview().offset(10.0)
            make.width.equalTo(55.0)
            make.height.equalTo(32.0)
        }
        
        self.slider.snp.makeConstraints { (make) in
            make.left.equalTo(previousChapter.snp.right).offset(10.0)
            make.right.equalTo(nextChapter.snp.left).offset(-10.0)
            make.top.equalToSuperview().offset(10.0)
            make.height.equalTo(32.0)
        }
        
        self.refreshButtonToggleLight()
    }
    
    /// 刷新 slider
    func sliderUpdate() {
        guard let readMenu_ = self.readMenu, let record = readMenu_.vc.record else { return }
        if let chapter = record.chapter {
            self.slider.maximumValue = Float(chapter.pages)
        }
        self.slider.value = Float(record.page + 1)
    }
    
    /// 点击按钮
    @objc private func clickButton(ges:UIGestureRecognizer?) {
        if let index = ges?.view?.tag {
            if index == 0 { // 目录
                self.clickCatalog()
            }else if index == 1 { // 日间夜间
                self.toggleLight()
            }else if index == 2 { // 设置
                self.clickSetup()
            }else{ // 下载
                self.clickDowload()
            }
        }
    }
    
    /// 上一章
    @objc func clickPreviousChapter() {
        guard let readMenu_ = self.readMenu else { return }
        self.readMenu?.delegate?.readMenuClickPreviousChapter?(readMenu: readMenu_)
    }
    
    /// 下一章
    @objc func clickNextChapter() {
        guard let readMenu_ = self.readMenu else { return }
        self.readMenu?.delegate?.readMenuClickNextChapter?(readMenu: readMenu_)
    }
    
    /// 目录
    func clickCatalog() {
        self.readMenu?.menuSH(isShow: false)
        self.readMenu?.leftView(isShow: true, complete: nil)
    }
    
    ///切换日间夜间模式
    func toggleLight() {
        guard let readMenu_ = self.readMenu else { return }
        let isNight = CLReadConfigure.shared.isNight
        CLReadConfigure.shared.isNight = !isNight
        self.readMenu?.delegate?.readMenuClickLightButton?(readMenu: readMenu_, isDay: !isNight)
        self.refreshButtonToggleLight()
    }
    
    ///刷新日间夜间按钮状态
    func refreshButtonToggleLight() {
        if CLReadConfigure.shared.isNight {
            self.buttonToggleLight?.icon.image = UIImage(named: "menu_nightrain", in: Bundle(for: type(of: self)), compatibleWith: nil)
            self.buttonToggleLight?.labelTitle.text = "夜间"
        } else {
            self.buttonToggleLight?.icon.image = UIImage(named: "menu_sun", in: Bundle(for: type(of: self)), compatibleWith: nil)
            self.buttonToggleLight?.labelTitle.text = "日间"
        }
    }
    
    /// 设置
    func clickSetup() {
        self.readMenu?.bottomView(isShow: false) { [weak self] ()->Void in
            self?.readMenu?.novelsSettingView(isShow: true , complete: nil)
        }
    }
    
    /// 下载
    func clickDowload() {
        guard let readMenu_ = self.readMenu else { return }
        self.readMenu?.delegate?.readMenuClickDownload?(readMenu: readMenu_)
    }
    
    // MARK: -- ASValueTrackingSliderDelegate
    func sliderWillHidePopUpView(_ slider: ASValueTrackingSlider!) {
        guard let readMenu_ = self.readMenu else { return }
        self.readMenu?.delegate?.readMenuSliderEndScroll?(readMenu: readMenu_, slider: slider)
    }
    
    private class MenuButton : UIView {
        
        lazy var icon = UIImageView()
        lazy var labelTitle = UILabel()
        
        init(imageName:String,title:String) {
            super.init(frame: CGRect.zero)
            self.backgroundColor = UIColor.clear
            
            let image = UIImage(named: imageName, in: Bundle(for: type(of: self)), compatibleWith: nil)
            self.icon.image = image
            icon.contentMode = .center
            self.addSubview(icon)
            icon.snp.makeConstraints { (make) in
                make.size.equalTo(30.0)
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(20.0)
            }
            
            labelTitle.font = UIFont.systemFont(ofSize: 12.0)
            labelTitle.textColor = UIColor.white
            labelTitle.text = title
            self.addSubview(labelTitle)
            labelTitle.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.top.equalTo(icon.snp.bottom).offset(3.0)
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
}

extension DZMRMBottomView : ASValueTrackingSliderDelegate {
    func sliderWillDisplayPopUpView(_ slider: ASValueTrackingSlider!) {
        
    }
}
